home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-08-16 | 8.7 KB | 288 lines | [TEXT/MPS ] |
- # ProtoGen
- # MPW Shell Script
- # Original ProtoGen by C.K. Haun
- # First revision by David D. Ely
- # Second revision by Gina Cherry • August 16, 1991
- # Copyright: © 1991 by Apple Computer, Inc., all rights reserved.
- #
- # Usage:
- # ProtoGen [<input file>…] [-e] [-n] [-o <output file>]
- #
- # Function:
- # Prototype generator for ANSI C source code.
- # Puts prototypes from the C source files specified on the command line into a prototype file.
- # If no input file is specified, ProtoGen operates on the target file. If no output file is specified,
- # the output defaults to <input file>.proto, using the first input file specified on the command line
- # for the output file name.
- # If -e is specified, all function declarations in the output file will be proceeded by extern.
- # If -o <output file> is specified, output will be appended to the named file. If -n is specified,
- # ProtoGen will create prototypes for old-style C source code.
- #
- # Note:
- # ProtoGen can be used with Commando.
-
- # Don't exit on error.
- Set Exit 0
-
- # Want searches to be case sensitive.
- Set CaseSensitive 1
-
- # Initialize the file variables.
- Set Files ""
- Set protoFile ""
- Set firstFile ""
-
- # Initially don't declare functions as externs.
- Set Extern 0
-
- # Initially set to create prototypes for ANSI C
- Set Ansi 1
-
- # Initialize Word variable.
- Set Word '[a-zA-Z_0-9]+'
-
- # Loop through parameters.
- Loop
- # Break if no more parameters.
- Break If "{1}" == ""
-
- If "{1}" == '-o'
- # Set protoFile to the specified output file if -o has not already been given as an option.
- If "{protoFile}" == ""
- # Get next parameter
- Shift 1
- # If there is no next parameter, print error message and exit program.
- If "{1}" == ""
- Echo "### {0}: Option ∂"{1}∂" must be followed by a file name."
- Echo "### Usage - ProtoGen [<input file>…] [-e] [-n] [-o <output file>]"
- Exit 1
- # Otherwise, set the output file to the next parameter.
- Else
- Set protoFile "{1}"
- End
- # Otherwise, write an error message and exit script.
- Else
- Echo "### "{0}": Option ∂"{1}∂" multiply defined."
- Echo "### Usage - ProtoGen [<input file>…] [-e] [-n] [-o <output file>]"
- Exit 1
- End
-
- # If -e option is specified, want to create prototypes as externs.
- Else If "{1}" == '-e'
- # Make sure Extern hasn't already been set.
- If {Extern} == 0
- Set Extern 1
- # If it has, write an error message and exit script.
- Else
- Echo "### {0}: Option ∂"{1}∂" multiply defined."
- Echo "### Usage - ProtoGen [<input file>…] [-e] [-n] [-o <output file>]"
- Exit 1
- End
-
- # If -n option is specified, want to prototype old-style C programs.
- Else If "{1}" == '-n'
- # Make sure Ansi hasn't already been set to 0.
- If {Ansi} == 1
- Set Ansi 0
- # If it has, write an error message and exit script.
- Else
- Echo "### {0}: Option ∂"{1}∂" multiply defined."
- Echo "### Usage - ProtoGen [<input file>…] [-e] [-n] [-o <output file>]"
- Exit 1
- End
-
- # If bad option is given, write error message and exit script.
- Else If "{1}" =~ /∂-≈/ || "{1}" == '-'
- Echo "### {0}: ∂"{1}∂" is not an option."
- Echo "### Usage - ProtoGen [<input file>…] [-e] [-n] [-o <output file>]"
- Exit 1
-
- # If parameter is not an option, add it to the list of input files.
- Else
- Set Files "{Files} '{1}'"
- # If this is the first file, set the firstFile variable
- If firstFile == ""
- Set firstFile "{1}"
- End
- End >> Dev:StdErr
-
- # Get next parameter.
- Shift 1
- End
-
- # If no input file was specified, set input file to the target file.
- If "{Files}" == ""
- Set Files "'{Target}'"
- End
-
- # If no output file was specified, set output file to <input file>.proto.
- If "{protoFile}" == ""
- If {firstFile} == ""
- Set protoFile "{Target}".proto
- Else
- Set protoFile "{firstFile}".proto
- End
- End
-
- # Create the output window by redirecting the output from the Echo command. If protoFile does not
- # exist, it will be created; otherwise, the output will be appended to the existing file.
- Echo "∂n∂/*----- ProtoGen prototypes - `date` -----*∂/∂n" >>"{protoFile}"
-
- # Make the output file the active window.
- Open "{protoFile}"
-
- # Mark end of output file.
- Find ∞ "{protoFile}"
- Mark -y § START "{protoFile}"
-
- For fName in {Files}
- # Record whether fName is already open.
- Set fullName "`Files -i -f "{fName}" ≥ Dev:Null`"
- Set wasOpen `Evaluate "∂`Windows∂`" =~ /≈{fullName}≈/`
-
- # Open the input file as the target window. Discard diagnostic output from the Open command,
- # since it's not needed.
- Open -t "{fName}" ≥ Dev:Null
-
- # If input file could not be opened, print error message and continue with next iteration of loop.
- If {Status} != 0
- Echo "### {0}: File {fName} not found." >> Dev:StdErr
- Continue
- End
-
- # Echo the name of the input file to the output file.
- Echo "∂n∂/*----- {fName} -----*∂/∂n" >>"{protoFile}"
-
- # Go to the top of the input file.
- Find • "{fName}"
-
- # Loop until there are no more function declarations in the input file.
- Loop
-
- # Position cursor at beginning of function.
- Find Δ/•{Word}[ ∂t∂n∂*a-zA-Z_0-9]*∂([ ∂t∂n∂*a-zA-Z_0-9∂[∂],]*[∂)][¬;∂)∂n]*∞/ "{fName}"
-
- # Break if no more functions.
- Break If {Status}
-
- # Select the entire line.
- Find !0 "{fName}"
-
- # If externs, write extern to input file.
- If {Extern}
- Echo -n "extern " >> "{protoFile}"
- End
-
- # Write function header to output file.
- Catenate "{fName}.§" >> "{protoFile}"
-
-
- # If prototyping old-style C file:
- If {Ansi} == 0
-
- # Position cursor after left parenthesis of current function in output file.
- Find \∂(\Δ "{protoFile}"
-
- # Loop through parameter list in the currently selected function in the output file.
- Loop
-
- # Select the next formal parameter name in the list.
- Find /[A-Za-z_0-9∂)]+/ "{protoFile}"
-
- # Break if have reached the right parenthesis.
- Break If " `Catenate "{protoFile}.§"`" =~ / *∂)/
-
- # If the parameter is a word ending in a right parenthesis, separate the word from
- # the parenthesis.
- If "`Catenate "{protoFile}.§"`" =~ /≈∂)/
- Find §¡0 "{protoFile}"
- Find /{Word}/ "{protoFile}"
- End
-
- # Use Evaluate statement to make the selected text in the output file (the name of the
- # formal parameter) available to the shell using the ®1 variable.
- (Evaluate "`Catenate "{protoFile}.§"`" =~ /({Word})®1/) ∑ Dev:Null
-
- # Mark the position of the current parameter in the output file.
- Mark § -y {0}.Param "{protoFile}"
-
- # Find the parameter name (including any '*'s associated with the parameter) in the
- # input file.
- Find /[∂*]*{®1}/ "{fName}"
-
- # Copy the full parameter name to the clipboard.
- Copy § "{fName}"
-
- # Write the full parameter name to the output file.
- Paste {0}.Param "{protoFile}"
-
- # Go to the beginning of the current line in the input file.
- Find \•\ "{fName}"
-
- # Find the first word on the line, which is the type of the current parameter.
- Find /{Word}[ ∂t]/ "{fName}"
-
- # Copy the parameter type to the clipboard.
- Copy § "{fName}"
-
- # Write the parameter type to the output file before the parameter name.
- Paste Δ{0}.Param "{protoFile}"
-
- # Position cursor at the end of the parameter name in the output file.
- Find {0}.ParamΔ "{protoFile}"
- End
- End
-
-
- # Insert a blank line after function header in the output file.
- Echo >> "{protoFile}"
-
- # Skip to end of function in the input file.
- Find /∂{/ "{fName}"
- MatchIt -c "{fName}"
-
- End
-
- # Close input file if file was not open before; don't save changes.
- If !"{wasOpen}"
- Close -n "{fName}"
- End
- End
-
- # Add a semicolon at the end of each line in the output file.
- Find ΔSTART "{protoFile}"
- Replace -c ∞ /∂)/ ");" "{protoFile}"
-
- # Insert void in function headers with an empty parameter list.
- Find ΔSTART "{protoFile}"
- Replace -c ∞ /∂([ ∂t]*∂)/ '(void)' "{protoFile}"
-
- # Insert tab between the function name and the left paren.
- Find ΔSTART "{protoFile}"
- Replace -c ∞ /({Word})®1[ ∂t]*∂(/ "®1∂t∂(" "{protoFile}"
-
- # Do some special formatting for old-style C prototypes.
- If {Ansi} == 0
-
- # Insert comma and space between all parameters.
- Find ΔSTART "{protoFile}"
- Replace -c ∞ /[,]({Word})®1/ ", ®1" "{protoFile}"
-
- # Remove extra tabs and spaces.
- Find ΔSTART "{protoFile}"
- Replace -c ∞ /({Word})®1[ ∂t]+([A-Za-z_0-9∂*]+)®2/ "®1 ®2" "{protoFile}"
-
- # Remove marker from the output file.
- Unmark {0}.Param "{protoFile}"
- End
-
- # Insert blank lines at the end of the output file.
- Echo "∂n∂n" >> "{protoFile}"
-
- # Remove the marker from the output file.
- Unmark START "{protoFile}"
-
- # Close output file; save changes.
- Close -y "{protoFile}"
-
-